package com.qmrssreader.system;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.qmrssreader.dto.RssItem;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/*
 *This class creates the ReallySimple reader Activity (which displays the main screen) and uses methods
 *to obtain and parse XML data from the given RSS feed.
 */

public class ReallySimpleReader {
	static RssItem selectedItem = null;
	static String RSSTitle = "http://vnexpress.net/rss/gl/trang-chu.rss";
	static String feedUrl;

	static boolean isLive;
	boolean wena = false;

	String url = feedUrl;
	//String name;
	String title;
	Date pubDate;

	public Date getPubDate() {
		return pubDate;
	}

	public void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	TextView tv;
	ListView rssView;
	ArrayList<RssItem> rSSItems = new ArrayList<RssItem>();
	ArrayList<RssItem> newItems;
	ArrayAdapter<RssItem> aa;

	private ReallySimpleReader() {
		// this.url=uri;
	}

	public ReallySimpleReader(String uri) {
		this.url = uri;
		//this.name = name;
	}

	public ArrayList<RssItem> getRSSItems1() {
		ArrayList<RssItem> vectParse = new ArrayList<RssItem>();
		try {
			URL url = new URL(this.url);
			InputStream istream = url.openStream();

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			Document doc = builder.parse(istream);
			doc.getDocumentElement().normalize();

			Node nTitle = doc.getElementsByTagName("title").item(0);
			setTitle(nTitle.getTextContent());
			setPubDate(new Date(doc.getElementsByTagName("pubDate").item(0)
					.getTextContent()));

			NodeList nList = doc.getElementsByTagName("item");
			//System.out.println();

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					//System.out.println("" + temp);
					RssItem objBean;
					Element eElement = (Element) nNode;

					objBean = new RssItem();
					vectParse.add(objBean);

					Element _titleE = (Element) eElement.getElementsByTagName(
							"title").item(0);
					Element _descriptionE = (Element) eElement
							.getElementsByTagName("description").item(0);
					Element _pubDateE = (Element) eElement
							.getElementsByTagName("pubDate").item(0);
					Element _linkE = (Element) eElement.getElementsByTagName(
							"link").item(0);

					objBean.setTitle(_titleE.getFirstChild().getNodeValue());
//					String noHTMLString = _descriptionE.getFirstChild()
//							.getNodeValue();
//					noHTMLString = noHTMLString.replaceAll("\\<.*?\\>", "");
					//objBean.setDescription(noHTMLString);
					objBean.setDescription(_descriptionE.getTextContent());
					objBean.setLink(_linkE.getFirstChild().getNodeValue());
					Date _pubDate = new Date(_pubDateE.getFirstChild()
							.getNodeValue());
					objBean.setPubDate(_pubDate);

				}
			}
			return vectParse;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public ArrayList<RssItem> getRSSItems(InputStream s) {
		ArrayList<RssItem> vectParse = new ArrayList<RssItem>();
		try {
			URL url = new URL(this.url);
			URLConnection con = url.openConnection();

			//System.out.println("Connection is : " + con);

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			//System.out.println("Reader :" + reader);

			String inputLine;
			String fullStr = "";
			while ((inputLine = reader.readLine()) != null)
				fullStr = fullStr.concat(inputLine + "\n");

			InputStream istream = url.openStream();

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			Document doc = builder.parse(istream);

			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("item");

			System.out.println();

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					//System.out.println("" + temp);
					RssItem objBean;
					Element eElement = (Element) nNode;

					objBean = new RssItem();
					vectParse.add(objBean);

					Element _titleE = (Element) eElement.getElementsByTagName(
							"title").item(0);
					Element _descriptionE = (Element) eElement
							.getElementsByTagName("description").item(0);
					Element _pubDateE = (Element) eElement
							.getElementsByTagName("pubDate").item(0);
					Element _linkE = (Element) eElement.getElementsByTagName(
							"link").item(0);

					objBean.setTitle(_titleE.getFirstChild().getNodeValue());
					String noHTMLString = _descriptionE.getFirstChild()
							.getNodeValue();
					noHTMLString = noHTMLString.replaceAll("\\<.*?\\>", "");
					objBean.setDescription(noHTMLString);
					objBean.setLink(_linkE.getFirstChild().getNodeValue());
					Date _pubDate = new Date(_pubDateE.getFirstChild()
							.getNodeValue());
					objBean.setPubDate(_pubDate);

				}
			}
			return vectParse;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * This method creates an HTTP connection with the remote RSS host pointed
	 * by the user-specified URL and returns the server's response as a stream
	 * of XML data.
	 */
	public InputStream connect() {
		try {
			URL url = new URL("http://vnexpress.net/rss/gl/kinh-doanh.rss");// this.url);
			RSSTitle = url.getHost();

			URLConnection conn = url.openConnection();
			//System.out.println("ssss:" + RSSTitle);
			int ass = -1;
			conn.setConnectTimeout(30000);
			conn.setDoInput(true);
			conn.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			//System.out.println("Reader :" + reader);
			// ass=conn.getResponseCode();

			//System.out.println("respone:" + ass);
			if (ass == HttpURLConnection.HTTP_OK) {
				//System.out.println("ssss:" + RSSTitle);
				InputStream is = conn.getInputStream();
				this.wena = true;
				return is;
			} else {

				return null;
			}
		} catch (Exception e) {
			//System.out.println("hahahahahhahhahha");
			return null;
		}

	}

}